package srdb.migration.dbhandler.db2;

import srdb.migration.Utility;
import srdb.migration.common.MTKTransformDDL;
import srdb.migration.schema.metadata.Column;
import srdb.migration.schema.metadata.Constraint;
import srdb.migration.schema.metadata.DataType;
import srdb.migration.schema.metadata.Domain;
import srdb.migration.schema.metadata.Function;
import srdb.migration.schema.metadata.Grant;
import srdb.migration.schema.metadata.Group;
import srdb.migration.schema.metadata.Index;
import srdb.migration.schema.metadata.Package;
import srdb.migration.schema.metadata.Procedure;
import srdb.migration.schema.metadata.Role;
import srdb.migration.schema.metadata.Rule;
import srdb.migration.schema.metadata.Synonym;
import srdb.migration.schema.metadata.Trigger;
import srdb.migration.schema.metadata.User;

public class DB2TransformDDL extends MTKTransformDDL {

	@Override
	public String getCreateScript(Procedure paramProcedure) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Function paramFunction) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Synonym paramSynonym) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Trigger paramTrigger) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Constraint constraint) {
		String sql = "";

		String constTableName = constraint.getTableName();

		if (constraint.getType() == 'P') {
			sql = sql + "ALTER TABLE " + constTableName + " ADD CONSTRAINT "
					+ constraint.getName(true) + " PRIMARY KEY (";

			for (int i = 0; i < constraint.getColumns().size(); i++) {
				sql = sql + constraint.getColumns().get(i).getName();
				if (i < constraint.getColumns().size() - 1)
					sql = sql + ", ";
			}
			sql = sql + ");\n";
		}

		if (constraint.getType() == 'U') {
			sql = sql + "ALTER TABLE " + constTableName + " ADD CONSTRAINT "
					+ constraint.getName(true) + " UNIQUE (";

			for (int i = 0; i < constraint.getColumns().size(); i++) {
				sql = sql + constraint.getColumns().get(i).getName();
				if (i < constraint.getColumns().size() - 1)
					sql = sql + ", ";
			}
			sql = sql + ");\n";
		}

		if (constraint.getType() == 'K') {
			if (!constraint.getSearchCondition().endsWith("IS NOT NULL")) {
				sql = sql + "ALTER TABLE " + constTableName
						+ " ADD CONSTRAINT " + constraint.getName(true)
						+ " CHECK (" + constraint.getSearchCondition();

				sql = sql + ");\n";
			}
		}

		if (constraint.getType() == 'F') {
			sql = sql + "ALTER TABLE " + constTableName + " ADD CONSTRAINT "
					+ constraint.getName(true) + " FOREIGN KEY (";

			for (int i = 0; i < constraint.getColumns().size(); i++) {
				sql = sql + constraint.getColumns().get(i).getName();
				if (i < constraint.getColumns().size() - 1)
					sql = sql + ", ";
			}
			sql = sql + ") REFERENCES "
					+ constraint.getRefIntegritySchemaQualifiedTableName()
					+ " (";
			for (int i = 0; i < constraint.getRefIntegrityColumns().size(); i++) {
				sql = sql
						+ constraint.getRefIntegrityColumns().get(i).getName();
				if (i < constraint.getRefIntegrityColumns().size() - 1)
					sql = sql + ", ";
			}
			sql = sql + ") MATCH FULL ON DELETE " + constraint.getDeleteRule()
					+ ";\n";
		}

		return replaceNames(sql, getSourceToTargetNames());
	}

	@Override
	public String getCreateScript(Index paramIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Role paramRole) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(User paramUser) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Grant paramGrant) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Rule paramRule) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Group paramGroup) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(Domain paramDomain) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScript(DataType paramDataType) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScriptPackageSpec(Package paramPackage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCreateScriptPackageBody(Package paramPackage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getColumnLine(Column column) {

		String str = "";
		String colName = column.getName();

		// colName = escape(colName, column.isCaseSensitive());

		str = str + getIndent() + colName;

		String customType = null;

		if ((customType = super.getCustomColType(column.getTableName(),
				column.getName())) != null) {
			str = str + " " + customType;
		} else {
			if (column.isRowID()) {
				str = str + " " + "VARCHAR";
			} else if (column.isInterval())
				str = str + " " + "INTERVAL";
			else if (column.isGUID())
				str = str + " UUID";
			else if (column.isLargeObject())
				str = str + " BYTEA";
			else if (column.isText())
				str = str + " TEXT";
			else if ((column.isVarchar())
					&& ((column.getDataTypeString()
							.equalsIgnoreCase("VARCHAR2")) || (column
							.getDataTypeString()
							.equalsIgnoreCase("CHAR VARYING")))) {
				str = str + " VARCHAR";
			} else if ((column.getDataTypeString().equalsIgnoreCase("GRAPHIC"))) {
				str = str + " VARCHAR";
			} else {
				str = str + " " + column.getDataTypeString();
			}

			if ((column.getDataType() == Column.DataType.VARCHAR)
					|| (column.getDataType() == Column.DataType.NVARCHAR)) {
				str = str + "(" + column.getLength() + ")";
			} else if (((column.getDataType() == Column.DataType.INTEGER)
					&& (!column.getDataTypeString().equalsIgnoreCase("INTEGER"))
					&& (!column.isSmallint())
					&& (!column.getDataTypeString().equalsIgnoreCase("INT"))
					&& (!column.getDataTypeString().equalsIgnoreCase("BIGINT"))
					&& (!column.getDataTypeString().equalsIgnoreCase("SERIAL"))
					&& (!column.getDataTypeString().equalsIgnoreCase(
							"BIGSERIAL")) && (!column.getDataTypeString()
					.equalsIgnoreCase("YEAR")))
					|| ((column.getDataType() == Column.DataType.NUMERIC) && (!column
							.isReal()))) {
				if (column.getPrecision() > 0) {
					str = str + "(" + column.getPrecision();
					if (column.getScale() > 0)
						str = str + "," + column.getScale();
					str = str + ")";
				}

			}

		}

		if ((column.getDefaultClause() != null)
				&& (column.getDefaultClause().trim().length() > 0)) {
			String defaultValue = column.getDefaultClause();
			if ((defaultValue != null)
					&& (defaultValue.toLowerCase().contains("current"))) {
				defaultValue = "now";
			}

			if (column.isTimeStamp()) {
				if (this.defaultDateTime != null) {
					defaultValue = this.defaultDateTime.replace("_", " ");
					if (defaultValue.length() == 10) {
						defaultValue = defaultValue + " 00:00:00";
					}

				} else if (defaultValue.equalsIgnoreCase("0000-00-00 00:00:00")) {
					try {
						defaultValue = Utility
								.formatMySQLZeroDateTimeToPG(defaultValue);
					} catch (Exception exp) {
					}
				}
			}
			if (column.isDate()) {
				if (this.defaultDateTime != null) {
					defaultValue = this.defaultDateTime.substring(0, 10);
				} else if (defaultValue.equalsIgnoreCase("0000-00-00")) {
					try {
						defaultValue = Utility
								.formatMySQLZeroDateTimeToPG(defaultValue);
					} catch (Exception exp) {
					}
				}
			}
			// if (!defaultValue.trim().toUpperCase().startsWith("DEFAULT ")) {
			str = str + " DEFAULT " + defaultValue + "";
			// } else {
			// defaultValue =
			// replaceDefaultClauseEnclosingDoubleQuotes(defaultValue);
			// str = str + " " + defaultValue;
			// }
		}

		if (!column.isNullable()) {
			str = str + " NOT NULL";
		}
		return str;
	}

}
